@page "/pages/authentication/register"
@using System.Text.RegularExpressions
@using CleanArchitecture.Blazor.Application.Common.Security
@using CleanArchitecture.Blazor.Infrastructure.Constants.Role
@using Microsoft.AspNetCore.Identity
@using System.ComponentModel.DataAnnotations
@using System.Security.Claims
@using FluentValidation;
@inject IStringLocalizer<Register> L
@attribute [AllowAnonymous]

<MudForm Model="@model" @ref="@form"  Validation="@(registerValidator.ValidateValue)">
<MudText Typo="Typo.h4" GutterBottom="true">@L["Sign Up"]</MudText>
<MudText>@L["have an account?"] <MudLink Href="/pages/authentication/login">@L["Sign In"]</MudLink></MudText>

<MudTextField T="string"  
    Immediate="true"
    Label="@L["User Name"]"
    @bind-Value="model.UserName" 
    For="@(() => model.UserName)" 
    Variant="Variant.Outlined" 
    Required="true"
    RequiredError="@L["user name is required!"]"
    Class="my-4"></MudTextField>

<MudTextField T="string" 
    Immediate="true"
    Label="@L["E-mail"]" 
    @bind-Value="model.Email" 
    For="@(() => model.Email)" 
    Required="true"
    RequiredError="@L["e-mail is required!"]"
    Variant="Variant.Outlined"></MudTextField>

<MudTextField 
     Immediate="true"
     Label="@L["Password"]" 
     @bind-Value="model.Password" 
     For="@(() => model.Password)"
     Variant="Variant.Outlined" 
     InputType="@PasswordInput" 
     Adornment="Adornment.End" 
     AdornmentIcon="@PasswordInputIcon" 
     Required="true"
     RequiredError="@L["password is required!"]"
     OnAdornmentClick="TogglePasswordVisibility" Class="mt-4" />
<MudTextField 
     Immediate="true"
     Label="@L["Confirm Password"]" 
     @bind-Value="model.ConfirmPassword" 
     For="@(() => model.ConfirmPassword)"
     Variant="Variant.Outlined" 
     InputType="@PasswordInput" 
     Adornment="Adornment.End" 
     AdornmentIcon="@PasswordInputIcon"
     Required="true"
     RequiredError="@L["password is required!"]"
     OnAdornmentClick="TogglePasswordVisibility" Class="mt-4" />

<MudCheckBox For="@(() => model.AgreeToTerms)" @bind-Checked="@model.AgreeToTerms" Label="@L["I agree to the terms and privacy"]" Color="Color.Primary" Class="ml-n1 my-3"></MudCheckBox>

<MudButton Variant="Variant.Filled" 
     Color="Color.Primary" 
     Disabled="@(!model.AgreeToTerms)"  
     Size="Size.Large" 
     OnClick="@(async () => await Submit())"
     FullWidth="true">@L["Register"]</MudButton>
</MudForm>
@code {
    [Inject] private NavigationManager _navigation { get; set; } = default!;
    [Inject] private UserManager<ApplicationUser> _userManager { get; set; }= default!;
    MudForm form;
    RegisterFormModel model = new ();
    RegisterFormModelFluentValidator registerValidator = new ();
   

    bool PasswordVisibility;
    InputType PasswordInput = InputType.Password;
    string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;

    void TogglePasswordVisibility()
    {
        @if (PasswordVisibility)
        {
            PasswordVisibility = false;
            PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
            PasswordInput = InputType.Password;
        }
        else
        {
            PasswordVisibility = true;
            PasswordInputIcon = Icons.Material.Filled.Visibility;
            PasswordInput = InputType.Text;
        }
    }
    private async Task Submit()
    {
        await form.Validate();

        if (form.IsValid)
        {
            var user = new ApplicationUser()
                {
                    UserName=model.UserName,
                    Email=model.Email
                };
            var result = await _userManager.CreateAsync(user,model.Password);
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, RoleConstants.BasicRole);
                _navigation.NavigateTo("/pages/authentication/login");
                Snackbar.Add(L["Register successfully!"], MudBlazor.Severity.Info);
            }
            else
            {
                Snackbar.Add($"{string.Join(",", result.Errors.Select(x=>x.Description))}", MudBlazor.Severity.Error);
            }

        }
    }
}
